using System;

namespace WegasManagementSystem.Models
{
    /// <summary>
    /// 工资信息类，用于工资计算和展示
    /// </summary>
    public class SalaryInfo
    {
        /// <summary>
        /// 员工ID
        /// </summary>
        public int EmpId { get; set; }

        /// <summary>
        /// 员工姓名
        /// </summary>
        public string EmpName { get; set; }

        /// <summary>
        /// 员工工号
        /// </summary>
        public string EmpNumber { get; set; }

        /// <summary>
        /// 部门ID
        /// </summary>
        public int DeptId { get; set; }

        /// <summary>
        /// 部门名称
        /// </summary>
        public string DeptName { get; set; }

        /// <summary>
        /// 职位
        /// </summary>
        public string Position { get; set; }

        /// <summary>
        /// 年份
        /// </summary>
        public int Year { get; set; }

        /// <summary>
        /// 月份
        /// </summary>
        public int Month { get; set; }

        /// <summary>
        /// 基本工资
        /// </summary>
        public decimal BaseSalary { get; set; }

        /// <summary>
        /// 岗位津贴
        /// </summary>
        public decimal PositionAllowance { get; set; }

        /// <summary>
        /// 绩效奖金
        /// </summary>
        public decimal PerformanceBonus { get; set; }

        /// <summary>
        /// 加班费
        /// </summary>
        public decimal OvertimePay { get; set; }

        /// <summary>
        /// 津贴
        /// </summary>
        public decimal Allowance { get; set; }

        /// <summary>
        /// 社保缴费
        /// </summary>
        public decimal SocialInsurance { get; set; }

        /// <summary>
        /// 公积金缴费
        /// </summary>
        public decimal HousingFund { get; set; }

        /// <summary>
        /// 个人所得税
        /// </summary>
        public decimal IncomeTax { get; set; }

        /// <summary>
        /// 缺勤扣款
        /// </summary>
        public decimal AbsenceDeduction { get; set; }

        /// <summary>
        /// 其他扣款
        /// </summary>
        public decimal OtherDeductions { get; set; }

        /// <summary>
        /// 总收入
        /// </summary>
        public decimal TotalIncome { get; set; }

        /// <summary>
        /// 总扣除
        /// </summary>
        public decimal TotalDeduction { get; set; }

        /// <summary>
        /// 实发工资
        /// </summary>
        public decimal NetSalary { get; set; }

        /// <summary>
        /// 考勤情况
        /// </summary>
        public string AttendanceInfo { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }

        /// <summary>
        /// 计算年月
        /// </summary>
        public string YearMonth
        {
            get { return $"{Year}-{Month:D2}"; }
            set
            {
                if (!string.IsNullOrEmpty(value) && value.Length >= 7)
                {
                    string[] parts = value.Split('-');
                    if (parts.Length == 2)
                    {
                        Year = Convert.ToInt32(parts[0]);
                        Month = Convert.ToInt32(parts[1]);
                    }
                }
            }
        }

        /// <summary>
        /// 计算日期
        /// </summary>
        public DateTime CalculateDate { get; set; }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public SalaryInfo()
        {
            Year = DateTime.Now.Year;
            Month = DateTime.Now.Month;
        }

        /// <summary>
        /// 计算总收入
        /// </summary>
        public void CalculateTotalIncome()
        {
            TotalIncome = BaseSalary + PositionAllowance + PerformanceBonus + OvertimePay + Allowance;
        }

        /// <summary>
        /// 计算总扣除
        /// </summary>
        public void CalculateTotalDeduction()
        {
            TotalDeduction = SocialInsurance + HousingFund + IncomeTax + AbsenceDeduction + OtherDeductions;
        }

        /// <summary>
        /// 计算实发工资
        /// </summary>
        public void CalculateNetSalary()
        {
            CalculateTotalIncome();
            CalculateTotalDeduction();
            NetSalary = TotalIncome - TotalDeduction;
        }

        /// <summary>
        /// 获取员工工资
        /// </summary>
        /// <param name="empId">员工ID</param>
        /// <param name="yearMonth">年月（格式：YYYY-MM）</param>
        /// <returns>工资信息</returns>
        public static SalaryInfo GetEmployeeSalary(int empId, string yearMonth)
        {
            if (empId <= 0)
                throw new ArgumentException("员工ID无效", nameof(empId));

            if (string.IsNullOrEmpty(yearMonth))
                throw new ArgumentException("年月不能为空", nameof(yearMonth));

            // 创建一个新的工资信息对象
            SalaryInfo salaryInfo = new SalaryInfo();
            
            // 解析年月
            string[] parts = yearMonth.Split('-');
            if (parts.Length == 2)
            {
                salaryInfo.Year = Convert.ToInt32(parts[0]);
                salaryInfo.Month = Convert.ToInt32(parts[1]);
            }
            
            salaryInfo.EmpId = empId;
            
            // 注意：这里只是创建一个基本的对象，实际的数据填充应该在BLL层完成
            
            return salaryInfo;
        }
    }
} 